package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ICrmCustomerDistributionRecordDAO;
import org.jsola.hr.dto.CrmCustomerDistributionRecordAddDTO;
import org.jsola.hr.dto.CrmCustomerDistributionRecordUpdateDTO;
import org.jsola.hr.entity.CrmCustomerDistributionRecordDO;
import org.jsola.hr.query.CrmCustomerDistributionRecordQuery;
import org.jsola.hr.service.ICrmCustomerDistributionRecordService;
import org.jsola.hr.vo.CrmCustomerDistributionRecordListVO;
import org.jsola.hr.vo.CrmCustomerDistributionRecordVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 客户分配记录表
 *
 * @author zhr
 */
@Service("hrCrmCustomerDistributionRecordServiceImpl")
public class CrmCustomerDistributionRecordServiceImpl implements ICrmCustomerDistributionRecordService {

    @Autowired
    private ICrmCustomerDistributionRecordDAO crmCustomerDistributionRecordDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public CrmCustomerDistributionRecordVO save(CrmCustomerDistributionRecordAddDTO crmCustomerDistributionRecordAddDTO, TokenUser tokenUser) {
        // 转DO
        CrmCustomerDistributionRecordDO crmCustomerDistributionRecordDO = crmCustomerDistributionRecordAddDTO.to(CrmCustomerDistributionRecordDO.class);
        // 保存
        crmCustomerDistributionRecordDO = save(crmCustomerDistributionRecordDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return crmCustomerDistributionRecordDO.to(CrmCustomerDistributionRecordVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(CrmCustomerDistributionRecordUpdateDTO crmCustomerDistributionRecordUpdateDTO, TokenUser tokenUser) {
        //转DO
        CrmCustomerDistributionRecordDO crmCustomerDistributionRecordDO = crmCustomerDistributionRecordUpdateDTO.to(CrmCustomerDistributionRecordDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(crmCustomerDistributionRecordDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... crmCustomerDistributionRecordIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) crmCustomerDistributionRecordIds);
    }


    @Override
    public CrmCustomerDistributionRecordVO selectById(String crmCustomerDistributionRecordId, String siteId) {
        CrmCustomerDistributionRecordDO crmCustomerDistributionRecordDO = selectDOById(crmCustomerDistributionRecordId, siteId);
        if (crmCustomerDistributionRecordDO == null) {
            return null;
        }
        return crmCustomerDistributionRecordDO.to(CrmCustomerDistributionRecordVO.class);
    }

    @Override
    public List<CrmCustomerDistributionRecordListVO> select(CrmCustomerDistributionRecordQuery crmCustomerDistributionRecordQuery, String siteId) {
        List<CrmCustomerDistributionRecordDO> crmCustomerDistributionRecordDOList = selectDO(crmCustomerDistributionRecordQuery, siteId);
        if(CollectionUtils.isEmpty(crmCustomerDistributionRecordDOList)) {
            return crmCustomerDistributionRecordDOList == null ? null : new ArrayList<>();
        }
        return crmCustomerDistributionRecordDOList.stream()
                .map(crmCustomerDistributionRecordDO -> crmCustomerDistributionRecordDO.to(CrmCustomerDistributionRecordListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(CrmCustomerDistributionRecordQuery crmCustomerDistributionRecordQuery, String siteId) {
        Example example = buildExample(crmCustomerDistributionRecordQuery,siteId);
        return crmCustomerDistributionRecordDAO.selectCountByExample(example);
    }

    @Override
    public Page<CrmCustomerDistributionRecordListVO> selectPage(CrmCustomerDistributionRecordQuery crmCustomerDistributionRecordQuery, String siteId) {
        Example example = buildExample(crmCustomerDistributionRecordQuery,siteId);
        Page<CrmCustomerDistributionRecordDO> page = crmCustomerDistributionRecordDAO.selectPageByExample(example,
                crmCustomerDistributionRecordQuery.getPageNo(),
                crmCustomerDistributionRecordQuery.getPageSize());

        return page.to(CrmCustomerDistributionRecordListVO.class);
    }

    @Override
    public CrmCustomerDistributionRecordDO selectDOById(String crmCustomerDistributionRecordId, String siteId) {
        return listById(crmCustomerDistributionRecordId, siteId);
    }

    @Override
    public List<CrmCustomerDistributionRecordDO> selectDO(CrmCustomerDistributionRecordQuery crmCustomerDistributionRecordQuery, String siteId) {
        Example example = buildExample(crmCustomerDistributionRecordQuery,siteId);
        return crmCustomerDistributionRecordDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param crmCustomerDistributionRecordQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(CrmCustomerDistributionRecordQuery crmCustomerDistributionRecordQuery, String siteId) {
        Example example = new Example(CrmCustomerDistributionRecordDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if(!StringUtils.isEmpty(crmCustomerDistributionRecordQuery.getCustomerId())){
            example.and().andEqualTo("customerId",crmCustomerDistributionRecordQuery.getCustomerId());
        }
        // 排序
        ExampleKit.setExampleOrder(example,crmCustomerDistributionRecordQuery.getOrders());
        return example;
    }
}




